package com.ruoyi.xds.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.xds.domain.HupRegion;
import com.ruoyi.xds.service.IHupRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 省市区Controller
 *
 * @author ruoyi
 * @date 2025-09-01
 */
@RestController
@RequestMapping("/system/region")
public class HupRegionController extends BaseController {
    @Autowired
    private IHupRegionService hupRegionService;

    /**
     * 查询省市区列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(HupRegion hupRegion) {
        startPage();
        List<HupRegion> list = hupRegionService.selectHupRegionList(hupRegion);
        return getDataTable(list);
    }

    /**
     * 导出省市区列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @Log(title = "省市区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HupRegion hupRegion) {
        List<HupRegion> list = hupRegionService.selectHupRegionList(hupRegion);
        ExcelUtil<HupRegion> util = new ExcelUtil<HupRegion>(HupRegion.class);
        util.exportExcel(response, list, "省市区数据");
    }

    /**
     * 获取省市区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hupRegionService.selectHupRegionById(id));
    }

    /**
     * 新增省市区
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "省市区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HupRegion hupRegion) {
        return toAjax(hupRegionService.insertHupRegion(hupRegion));
    }

    /**
     * 修改省市区
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "省市区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HupRegion hupRegion) {
        return toAjax(hupRegionService.updateHupRegion(hupRegion));
    }

    /**
     * 删除省市区
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "省市区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hupRegionService.deleteHupRegionByIds(ids));
    }
}
